# -*- coding: utf-8 -*-
# @Time    : 2024/5/30 11:54
# @Author  : Liaogan
# @FileName: test0530.py

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


# Function to generate heart shape points
def heart_shape(t):
    x = 16 * np.sin(t) ** 3
    y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
    return x, y


# Generate points along the heart shape
t = np.linspace(0, 2 * np.pi, 1000)
x, y = heart_shape(t)

# Initialize the figure and axis
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.axis('equal')
ax.axis('off')
line, = ax.plot([], [], 'r', lw=2)
scatter = ax.scatter([], [], c=[], cmap='rainbow', alpha=0.6, s=2)


# Initialization function
def init():
    line.set_data([], [])
    scatter.set_offsets(np.c_[[], []])
    scatter.set_array(np.array([]))
    return line, scatter


# Animation function
def animate(i):
    # Generate random points around the heart shape
    num_fireworks_points = 500
    random_t = np.random.uniform(0, 2 * np.pi, num_fireworks_points)
    random_r = np.random.uniform(0, 1, num_fireworks_points) ** 0.5
    random_x = 16 * np.sin(random_t) ** 3 * random_r
    random_y = (13 * np.cos(random_t) - 5 * np.cos(2 * random_t) - 2 * np.cos(3 * random_t) - np.cos(4 * random_t)) * random_r

    line.set_data(x, y)
    scatter.set_offsets(np.c_[random_x, random_y])
    scatter.set_array(np.random.rand(num_fireworks_points))
    return line, scatter


# Create the animation
ani = FuncAnimation(fig, animate, init_func=init, frames=100, interval=50, blit=True)

# Save the animation as a GIF file
ani.save('firework_heart.gif', writer='pillow')

# To display the animation inline in a Jupyter notebook (uncomment if running in Jupyter)
# from IPython.display import HTML
# HTML(ani.to_jshtml())
